package app.nexd.com.indoor.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;

import app.nexd.com.indoor.R;
import app.nexd.com.indoor.activity.inter.IBDMapViewActivity;
import app.nexd.com.indoor.presenter.IRoutePlanPresenterImpl;
import app.nexd.com.indoor.presenter.inter.IRoutePlanPresenter;

/**
 * 用来设定路径规划路线的显示
 * <p>
 * Created by lawrence on 2015/12/17.
 */
public class RoutePlanActivity extends Activity implements View.OnClickListener, IBDMapViewActivity {

    private MapView mapView;
    private TextView centerTextView;
    private ImageButton leftBack;
    private BaiduMap baiduMap;
    private IRoutePlanPresenter iRoutePlanPresenter;
    private LatLng start;
    private LatLng end;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.bd_route_plan);
        mapView = (MapView) this.findViewById(R.id.bmapView);
        baiduMap = mapView.getMap();
        centerTextView = (TextView) this.findViewById(R.id.title_filter_text);
        centerTextView.setText("路径规划");
        leftBack = (ImageButton) this.findViewById(R.id.title_left_btn);
        leftBack.setOnClickListener(this);
        mapView.showZoomControls(false);
        mapView.showScaleControl(false);
        iRoutePlanPresenter = new IRoutePlanPresenterImpl(this, mapView, this);
        iRoutePlanPresenter.initMap();

        Intent intent = getIntent();
        start = intent.getParcelableExtra("start");
        end = intent.getParcelableExtra("end");
        if (start == null || end == null) {
            showToast("路径规划失败，缺少目的地!");
        } else {
            iRoutePlanPresenter.designRoutePlan(start, end); // 当起点和终点都不为空时， 开始进行步行路径规划
        }
        iRoutePlanPresenter.startLocation();
    }

    @Override
    protected void onDestroy() {
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        iRoutePlanPresenter.stopLocation();
        // 关闭定位图层
        baiduMap.setMyLocationEnabled(false);
        mapView.onDestroy();
        mapView = null;
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mapView.onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mapView.onPause();
        super.onPause();
    }

    @Override
    protected void onStop() {
        iRoutePlanPresenter.stopLocation();
        super.onStop();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_left_btn:
                this.finish();
                break;
        }
    }

    @Override
    public void setLocationData(MyLocationData locationData) {
        baiduMap.setMyLocationData(locationData);
    }


    //TODO 这里的添加图层，可以合并成一个图层
    @Override
    public void addOverlay(OverlayOptions options) {
        baiduMap.addOverlay(options);
    }

    @Override
    public void animateMapStatus(MapStatusUpdate mapStatusUpdate) {
        baiduMap.animateMapStatus(mapStatusUpdate);
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }
}
